<?php
class cForm
        {
        var $properties;//arrays (name=>value, value) (mixed array)
        var $select_tag_properties; //$default, $arrays, $default_name="", $restore_default=false
        function getProperties()
                {
                $this->properties["class"]=($this->properties["class"])?$this->properties["class"]:"forminput";
                foreach($this->properties as $key=>$value)
                        {
			$value = htmlentities(stripslashes($value));
                        $property.=(!is_int($key))?$key."="."\"$value\" ":" $value ";
                        }
		unset($this->properties);
                return $property;
                }

        function startForm()
                {
                return "<form ".$this->getProperties()." >";
                }

        function generateInputTag()
                {
                return "<input ".$this->getProperties()." >";
                }
	 function generateSelectMenu($name, $default, $arrays=array(),$default_name="",$size="",$restore_default=false,$event="", $width_css_class="")
                {
                if($size)
                        $multiple="size=\"$size\" multiple";
                $select_code="<select class=\"$width_css_class\" name=\"$name\" $multiple $event>";
                if (!$size && $default=="" || $restore_default==true)
                        {
                        if(!$default_name)
                                $default_name="--Select--";
                        if ($default=="" || $default[0]=="")
                                $selected="selected";
                        $select_code.="<option value=\"\" $selected>$default_name</option>";
                        }
                if($arrays)
                        {
                        for($i=0;$i< count($arrays);$i++)
                                $names[]=array_values($arrays[$i]);

                        foreach($names as $key=>$value)
                                {
                                $value_array[]=$value[0];
                                if($value[1])
                                        $name_array[]=$value[1];
                                else
                                        $name_array[]=$value[0];
                                }


                        // if multiple values are given as default show them as selected
                        if($multiple && $default)
                                {
                                for($i=0;$i< count($name_array);$i++)
                                        {
                                        $select_code.="<option value=\"$value_array[$i]\"";
					if(is_array($default))
					{
                                        foreach($default as $default_value)
                                                {
                                                if($value_array[$i]==$default_value)
                                                        $select_code.=" selected ";
                                                }
					}
					elseif($i==0)
					{
						$select_code.=" selected ";
					}
                                        $select_code.="> $name_array[$i] </option>";
                                        }
                                }
                        else // show items in the select menu and show the default value if given
                                {
                                for($i=0;$i< count($name_array);$i++)
                                        {
                                        if ($value_array[$i]==$default)
                                                $select_code.="<option value=\"$value_array[$i]\" selected> $name_array[$i] </option>";
                                        else
                                                $select_code.="<option value=\"$value_array[$i]\"> $name_array[$i] </option>";
                                        }
                                }
                        }
                $select_code.="</select>";
                if ($this->errors[$name])
                        $select_code.=$this->errors[$name];
                return $select_code;
                }
		 function displayLabel($label1)
                {
                $this->form_label=$label1;
                echo "<b>$this->form_label </b>";
                }
        function generateSelectTag()
                {
                $select_code="<select ".$this->getProperties()." >";
                if ($this->select_tag_properties["default"]=="" || $this->select_tag_properties["restore_default"]==true)
                        {
                        $this->select_tag_properties["default_name"]=($this->select_tag_properties["default_name"])?$this->select_tag_properties["default_name"]:"--Select--";
                        $select_code.="<option value=\"\" ";
                        $select_code.=($this->select_tag_properties["default"]=="" || $this->select_tag_properties["default"][0]=="")?" selected ":"";
                        $select_code.=">".$this->select_tag_properties["default_name"]."</option>";
                        }
                if($this->select_tag_properties["arrays"])
                        {                
                        for($i=0;$i< count($this->select_tag_properties["arrays"]);$i++)
                                $names[]=array_values($this->select_tag_properties["arrays"][$i]);

                        foreach($names as $key=>$value)
                                {
                                $value_array[]=$value[0];
                                $name_array[]=($value[2])?$value[2]:$value[0];
                                }

                        for($i=0;$i< count($name_array);$i++)
                                {
                                $select_code.="<option value=\"$value_array[$i]\" ";
                                $select_code.=($this->select_tag_properties["default"] && is_array($this->select_tag_properties["default"]))?((in_array($value_array[$i],$this->select_tag_properties["default"]))?"selected":""):(($value_array[$i]==$this->select_tag_properties["default"])?"selected":"");
                                $select_code.=" >$name_array[$i]</option>";
                                }
                        }
                $select_code.="</select>";
                return $select_code;
                }
                        function trimValues(&$form)
                {
                //Removes any leading white spaces.
                if($form)
                        {
                        while(list($key,$value) = each($form))
                                $form[$key]=trim($value);
                        }
                }
		
		 function validate(&$values)
                {
                global $form_locale;
                $this->errors=array();
                foreach($this->validations as $name=>$validateFor)
                        {
                        $val=$values[$name];
                        if (isset($validateFor[isEmpty]) && $val=="")
                                {
                                if (!empty($validateFor[isEmpty]))
                                        $errors=$validateFor[isEmpty];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isEmpty"]." ".$this->form_label;
                                }
                        elseif (isset($validateFor[isPrice]) && $this->checkPrice($val)==false)
                                {
                                if (!empty($validateFor[isPrice]))
                                        $errors=$validateFor[isPrice];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isPrice"];
                                }
                        elseif (isset($validateFor[isNegative]) && $this->checkNegative($val)==true)
                                {
                                if (!empty($validateFor[isNegative]))
                                        $errors=$validateFor[isNegative];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isNegative"];
                                }
                        elseif (isset($validateFor[isZero]) && $this->checkZero($val)==true)
                                {
                                if (!empty($validateFor[isZero]))
                                        $errors=$validateFor[isZero];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isZero"];
                                }
                        elseif ( isset($validateFor[isNumeric]) && !is_numeric($val))
                                {
                                if (!empty($validateFor[isNumeric]))
                                        $errors=$validateFor[isNumeric];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isNumeric"];
                                }
                        elseif (isset($validateFor[isInt]) && $this->checkInt($val) == false)
                                {
                                if (!empty($validateFor[isInt]))
                                        $errors=$validateFor[isInt];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isInt"];
                                }
                        elseif (isset($validateFor[isEmail]) && !$this->checkEmail($val))
                                {
                                if (!empty($validateFor[isEmail]))
                                        $errors=$validateFor[isEmail];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isEmail"];
                                }
                        elseif (isset($validateFor[isDate]) && !$this->checkValidDate($val))
                                {
                                if (!empty($validateFor[isDate]))
                                        $errors=$validateFor[isDate];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isDate"];
                                }
                        elseif (isset($validateFor[isPreferredDate]) && !$this->checkPreferredValidDate($val))
                                {
                                if (!empty($validateFor[isDate]))
                                        $errors=$validateFor[isDate];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isDate"];
                                }
                        elseif (isset($validateFor[isTime]) && !$this->checkValidTime($val))
                                {
                                if (!empty($validateFor[isTime]))
                                        $errors=$validateFor[isTime];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["isTime"];
                                }
                        elseif (isset($validateFor[checkPassword]) && !$this->checkLength($val, 5, 15))
                                {
                                if (!empty($validateFor[checkPassword]))
                                        $errors=$validateFor[checkPassword];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["Password_Length"];
                                }
                        elseif (isset($validateFor[checkLogin]) && !$this->checkLength($val, 5, 20))
                                {
                                if (!empty($validateFor[checkLogin]))
                                        $errors=$validateFor[checkLogin];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["Login_Length"];

                                }
                        elseif (isset($validateFor[checkPassword]) && !$this->checkAllowableCharacters($val))
                                {
                                if (!empty($validateFor[checkPassword]))
                                        $errors=$validateFor[checkPassword];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["Password_Char"];
                                }
                        elseif (isset($validateFor[checkLogin]) && !$this->checkAllowableCharacters($val))
                                {
                                if (!empty($validateFor[checkLogin]))
                                        $errors=$validateFor[checkLogin];
                                else
                                        $errors=$form_locale->ERRORS["form_php.inc"]["Login_Char"];
                                }
                        else
                                $errors="";
                        if ($errors)
                                $this->errors[$name]="<div class=\"error\">".$errors."</div>";
                        }
                }

        function endForm()
                {
                return "</form>";
                }                
        }
?>
